const {
  saveBoxes,
  eraseBoxById,
  getBoxByID,
  getBoxes,
  eraseBoxes,
  batchAddBoxes
} = require("../services/boxes.service.js");
const err = require("../constant/err.type.js");

class BoxesController {
  async saveBox(ctx, next) {
    var res;
    try {
      const { ...boxes } = ctx.request.body;
      if (!boxes.BoxID) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "保存接头盒失败", data: "" }),
          ctx
        );
        return;
      }
      res = await saveBoxes(boxes);
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "保存接头盒失败", data: "" }),
          ctx
        );
        return;
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
        return;
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "保存接头盒时出现错误，代码为" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }
  async createBox(ctx, next) {
    var res;
    try {
      const { ...boxes } = ctx.request.body;
      res = await saveBoxes(boxes);
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "创建接头盒失败", data: "" }),
          ctx
        );
        return;
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
        return;
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "保存接头盒时出现错误" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }
  async batchAddBoxes(ctx,next){
    var res;
    var rs;
    try {
      const {linkId,arr} = ctx.request.body;
      if(!(arr instanceof Array) || arr.length == 0){
        ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"导入数据错误，数据不能为空",data:""}),ctx);
        return;
      }
      rs = await eraseBoxes({linkId});
      res = await batchAddBoxes(arr);
      if(!res){
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "导入数据失败", data: "" }),
          ctx
        );
        return;
      }else{
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
        return;
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "导入数据出错" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }  
  async eraseBox(ctx, next) {
    var res;
    var { linkId } = ctx.request.body;
    if (!linkId) {
      ctx.app.emit(
        "error",
        err.setDefaultValue({ code: -1, msg: "线路ID错误", data: "" }),
        ctx
      );
      return;
    }
    try {
      res = await eraseBoxes({linkId});
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "删除接头盒失败", data: "" }),
          ctx
        );
        return;
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "删除接头盒时出现错误" + error,
          data: "",
        }),
        ctx
      );
      return;
    }
  }
  async getBoxes(ctx, next) {
    var { BoxID,linkId, BoxName} = ctx.request.body;
    var whereOpt = {};
    var res;
    try {
      BoxID > 0 && Object.assign(whereOpt, { BoxID });
      BoxName && Object.assign(whereOpt, { BoxName });
      linkId && Object.assign(whereOpt, { linkId }); 
      res = await getBoxes(whereOpt);
      if (!res) {
        ctx.app.emit("error", err.companySearchError, ctx);
        return;
      }
      ctx.status = 200;
      ctx.app.emit(
        "error",
        err.setDefaultValue({ code: 0, msg: "ok", data: res }),
        ctx
      );
    } catch (error) {
      console.log(error);
      ctx.app.emit("error", err.companySearchError, ctx);
      return;
    }
  }
  async getBoxById(ctx, next) {
    var { ID} = ctx.request.body;
    var res;
    try {
      res = await getBoxByID(ID);
      if (!res) {
        ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取接头盒失败", data: "" }), ctx);
        return;
      }
      ctx.status = 200;
      ctx.app.emit(
        "error",
        err.setDefaultValue({ code: 0, msg: "ok", data: res }),
        ctx
      );
    } catch (error) {
      console.log(error);
      ctx.app.emit("error", err.companySearchError, ctx);
      return;
    }
  }
}

module.exports = new BoxesController();
